import {
	pushCode
} from "@/api/Interface/login"
import {
	VerCodeTypeEnum
} from "@/enums/verCode"
import BasicConfig from "@/utils/config"

export const useCheckModule = (vm, moduleEnum) => {
	vm.$emit("check", {
		enumKey: moduleEnum
	})
}

export const useSendCode = async ({
	phone,
	flag = VerCodeTypeEnum["忘记密码"],
	url,
	toast = true,
	jump = true,
}) => {
	return new Promise((resolve, reject) => {
		if (uni.$u.test.mobile(phone)) {
			const to = url ?? `/pages/password/password?type=${VerCodeTypeEnum['忘记密码']}&phone=${phone}`
			pushCode({
				phone,
				flag
			}).then(res => {
				if (res.code === 200) {
					resolve({
						code: 200,
						msg: "验证码发送成功"
					})
					if (toast) BasicConfig.toast("验证码发送成功")
					if (jump) {
						setTimeout(() => {
							uni.navigateTo({
								url: to
							})
						}, toast ? 2000 : 100)
					}
				}
			})
		} else {
			if (toast) BasicConfig.toast(`接收验证码的手机格式不正确[${phone}]`)
			reject({
				code: 500,
				msg: `接收验证码的手机格式不正确[${phone}]`
			})
		}
	})
}